<template>
  <div id="add_attribute" class="Row_content" style="width:400px;">
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="110">
      <FormItem label="属性名称：" prop="packageName">
        <Input v-model="formValidate.packageName" placeholder="请输入" class='input-wid' :maxlength='10'></Input>
      </FormItem>
      <FormItem label="是否显示：" prop="isShow">
        <RadioGroup v-model="formValidate.isShow" size="large">
          <Radio label="1">显示</Radio>
          <Radio label="0" style="margin-left:50px">隐藏</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem style="margin-top: 36px">
        <Button type="primary" class="btn" @click="handleSubmit">确定</Button>
        <Button class="btn" style="margin-left: 24px" @click="handleReset">取消</Button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
import { addAttribute } from '@/api/commondity/index'
import { routeEqual } from '@/libs/util'
export default {
  name: 'add_attribute',
  data () {
    return {
      submitLock: false,
      formValidate: {
        packageName: '',
        isShow: '1'
      },
      ruleValidate: {
        packageName: { required: true, message: '请填写属性名称', trigger: 'blur' },
        isShow: { required: true, message: '请选择是否显示', trigger: 'change' }
      }
    }
  },
  methods: {
    handleSubmit () {
      this.$refs.formValidate.validate(valid => {
        if (this.submitLock) {
          return
        }
        this.submitLock = true
        if (valid) {
          addAttribute({
            ...this.formValidate,
            merchantId: this.$store.state.user.merchantId
          }).then(res => {
            this.submitLock = false
            if (res.data.status === 200) {
              this.$Message.success('新增成功!')
              this.handleReset()
            } else {
              this.$Message.error(`${res.data.error}`)
            }
          }).catch(err => {
            this.submitLock = false
            this.$Message.error(`${err}`)
          })
        }
      })
    },
    handleReset () {
      this.$store.commit('closeTag', this.$route)
      this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      this.$nextTick(() => {
        this.$router.push({
          name: 'attribute_list'
        })
      })
    }
  }
}
</script>

<style lang="less">
  #add_attribute{
    .btn{width: 100px}
  }
</style>
